import Cookies from 'js-cookie';
import config from '@/common/config';

const cookies = {};
const DEFAULT_COOKIE_OPTIONS = {
  path: '',
  expires: 1
};

/**
 * @description 存储 cookie 值
 * @param {String} name cookie name
 * @param {String} value cookie value
 * @param {Object} setting cookie setting
 */
cookies.set = function(name = 'default', value = '', cookieSetting = {}) {
  // 设置cookie的 配置属性  主要为 path : 路径  expires : 生命周期
  let currentCookieSetting = {
    ...DEFAULT_COOKIE_OPTIONS,
    ...config.cookie,
    expires: 1
  };
  Object.assign(currentCookieSetting, cookieSetting);
  Cookies.set(`g2-${config.releases.version}-${name}`, value, currentCookieSetting);
};

/**
 * @description 拿到 cookie 值
 * @param {String} name cookie name
 */
cookies.get = function(name = 'default', noPrefix = false) {
  return Cookies.get(noPrefix ? name : `g2-${config.releases.version}-${name}`);
};

/**
 * @description 拿到 cookie 全部的值
 */
cookies.getAll = function() {
  return Cookies.get();
};

/**
 * @description 删除 cookie
 * @param {String} name cookie name
 */
cookies.remove = function(name = 'default') {
  return Cookies.remove(`g2-${config.releases.version}-${name}`);
};

export default cookies;
